<?php
class OrderModel extends Model {
	

	//根据角色和门店读取对应的配送员
	public function getUserRoleOutle($map){
		$user = M("User");
		/* $map['role'] = $role;
		$map['outletid'] = $outlet ; */
		return $user->where($map)->field("id,username,realname")->select();
 	}
	
	//保存操作记录
	public function saveLog($map){
		
		$admin = M('Admin')->find(session(C('USER_AUTH_KEY')));
		$map['addname'] = $admin['realname'];
		$map['addtime'] = time();
		$map['addid']   = session(C('USER_AUTH_KEY'));

		$model = M("OrderLog");
		return $model->add($map);
	}
	
	//订单关联门店
	public function orderOutlet($map){
		$map['addtime'] = time();
		$map['addid']   = session(C('USER_AUTH_KEY'));
		$model = M("OrderOutlet");
		$model->add($map);
	}
	
	//取消订单关联门店
	public function delOutlet($map){
		
		$model = M("OrderOutlet");
		$model->where($map)->delete();
	}
	
	//订单关联配送员
	public function orderDelivery($map){
		$map['addtime'] = time();
		$map['addid']   = session(C('USER_AUTH_KEY'));
		$model = M("OrderDelivery");
		$model->add($map);
	}
	
	//获取用户的角色权限
	public function	getUserRole($role){
		
		if($role==1){
			$begin = strtotime(date("Y-m-d"));
			$end   = strtotime(date("Y-m-d")." 23:59:59");
			$map['addtime'] = array(array("egt",$begin),array("elt",$end));
		}elseif($role==2){
			$arr = $this->getWeek();
			$map['addtime'] = array(array("egt",$arr['begin']),array("elt",$arr['end']));
		}elseif($role==3){
			$arr = $this->getMouth();
			$map['addtime'] = array(array("egt",$arr['begin']),array("elt",$arr['end']));
		}
		return $map;
	}
	
	/**
	 * 获取本月时间戳
	 */
	public function getMouth(){
	
		$begin = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
		$end   = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
	
		return array("begin"=>strtotime($begin),"end"=>strtotime($end));
	}
	
	/**
	 * 获取本周时间戳
	 */
	public function getWeek(){
		$year = date("Y");
		$month = date("m");
		$day = date('w');
		$nowMonthDay = date("t");
	
		$firstday = date('d') - $day;
		if(substr($firstday,0,1) == "-"){
			$firstMonth = $month - 1;
			$lastMonthDay = date("t",$firstMonth);
			$firstday = $lastMonthDay - substr($firstday,1);
			$time_1 = strtotime($year."-".$firstMonth."-".$firstday." 23:59:59");
		}else{
			$time_1 = strtotime($year."-".$month."-".$firstday." 23:59:59");
		}
			
		$lastday = date('d') + (7 - $day);
		if($lastday > $nowMonthDay){
			$lastday = $lastday - $nowMonthDay;
			$lastMonth = $month + 1;
			$time_2 = strtotime($year."-".$lastMonth."-".$lastday." 23:59:59");
		}else{
			$time_2 = strtotime($year."-".$month."-".$lastday." 23:59:59");
		}
	
		return array("begin"=>$time_1,"end"=>$time_2);
	}
	
	/**
	 * 数据统计
	 */
	public function getOrderTotal($list){
		
		$arr        = array();
		$settlefee  = 0; //结算金额
		$receivefee = 0; //实收总金额
		$orderfee   = 0; //订单总金额
		$shipfee    = 0; //服务总金额
		$drinkfee   = 0; //酒水总金额
		$dishesfee  = 0; //菜品总金额
		
		foreach($list as $k=>$v){
			
			$settlefee  += $v['settlefee'];
			$orderfee   += $v['orderfee'];
			$shipfee    += $v['shipfee']+$v['exemptfee'];
			$dishesfee  += $v['dishesfee'];
			$drinkfee   += $v['drinkfee'];
			$receivefee += $v['receivefee'];
		}
		
		$arr['num']        = count($list);
		$arr['settlefee']  = ceil($settlefee);
		$arr['orderfee']   = ceil($orderfee);
		$arr['shipfee']    = ceil($shipfee);
		$arr['drinkfee']   = ceil($drinkfee);
		$arr['dishesfee']  = ceil($dishesfee);
		$arr['receivefee'] = ceil($receivefee);
		
		return $arr;
	}
	
}
		

?>